class ItemCategory < ActiveRecord::Base
  belongs_to :parent, :class_name => 'ItemCategory', :foreign_key => 'parent_id'
  has_many :children, :class_name => 'ItemCategory', :foreign_key => 'parent_id', :order => :sort_order
  has_many :cat1_items, :class_name => 'Item', :foreign_key => :cat1_id
  has_many :cat2_items, :class_name => 'Item', :foreign_key => :cat2_id
  has_many :cat1_tags, :source => :item_tags, :through => :cat1_items
  has_many :cat2_tags, :source => :item_tags, :through => :cat2_items
  has_many :brands, :through => :cat2_items
  has_many :styles, :source => :item_style, :through => :cat2_items
    
  #attr_accessible :name, :parent_id, :cat_level, :is_parent, :sort_order, :status, :flag, :permalink
  
  validates_presence_of :name
  validates_uniqueness_of :name
  validates_numericality_of :cat_level
  
  named_scope :cat1, :conditions => ["parent_id IS ?", nil], :order => :sort_order
  named_scope :cat2, :conditions => ["parent_id IS NOT ?", nil], :order => :sort_order
  
  def items
    cat1_items + cat2_items
  end
  
  def to_s
    name
  end
  
  def to_param
    "#{id}-#{name}"
  end
end
